﻿using eLoan.Models;
using System;
using System.Linq;
using System.Web.Mvc;

namespace eLoan.Controllers
{
    public class LoanCollateralController : Controller
    {
        public ActionResult AddEdit(int? id, string documentNo)
        {
            try
            {
                var db = new DatabaseContext();

                var loan = db.Loans.Where(w => w.DocumentNo == documentNo).SingleOrDefault<Loan>();
                var model = new LoanCollateralModel();

                model.DocumentNo = documentNo;
                model.BorrowerID = loan.BorrowerID;
                model.BorrowerName = AdministrationHelper.GetBorrowerName(loan.BorrowerID);
                model.BranchCode = loan.BranchCode;

                if (id == null)
                {
                    model.DBCommand = AdministrationHelper.DBCommand.Add.ToString();
                    model.LoanCollateral = new LoanCollateral();
                    model.LoanCollateral.DocumentNo = documentNo;
                    model.LoanCollateral.MakeOrType = string.Empty;
                    model.LoanCollateral.BrandOrFileNo = string.Empty;
                    model.LoanCollateral.SerialOrPlateNo = string.Empty;
                    model.LoanCollateral.Amount = 0;

                    return View(model);
                }
                else
                {
                    model.DBCommand = AdministrationHelper.DBCommand.Edit.ToString();
                    model.LoanCollateral = db.LoanCollaterals.Where(w => w.ID == id).SingleOrDefault<LoanCollateral>();

                    return View(model);
                }
            }
            catch (Exception ex)
            {
                return View("Error", ex);
            }
        }

        public ActionResult Delete(int? id, string documentNo)
        {
            try
            {
                var db = new DatabaseContext();
                var loan = db.Loans.Where(w => w.DocumentNo == documentNo).SingleOrDefault<Loan>();
                var model = new LoanCollateralModel();

                model.DocumentNo = documentNo;
                model.BorrowerID = loan.BorrowerID;
                model.BorrowerName = AdministrationHelper.GetBorrowerName(loan.BorrowerID);
                model.BranchCode = loan.BranchCode;

                model.LoanCollateral = db.LoanCollaterals.Where(w => w.ID == id).SingleOrDefault<LoanCollateral>();

                return View(model);
            }
            catch (Exception ex)
            {
                return View("Error", ex);
            }
        }

        [HttpPost]
        public ActionResult DeleteLoanCollateral(int? id, string documentNo, int? borrowerID, string branchCode, string command)
        {
            try
            {
                if (command == AdministrationHelper.DBCommand.Delete.ToString())
                {
                    using (var db = new DatabaseContext())
                    {
                        var collateral = db.LoanCollaterals.Where(w => w.ID == id).SingleOrDefault<LoanCollateral>();
                        db.LoanCollaterals.Remove(collateral);
                        db.SaveChanges();
                    }
                }
                else
                {
                    // cancelled
                }

                return RedirectToAction("LoanCoMakersAndCollaterals", "Loan", new { documentNo = documentNo, borrowerID = borrowerID, branchCode = branchCode });
            }
            catch (Exception ex)
            {
                return View("Error", ex);
            }
        }

        [HttpPost]
        public ActionResult Save(LoanCollateralModel model, string command)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    using (var db = new DatabaseContext())
                    {
                        if (command == AdministrationHelper.DBCommand.Add.ToString())
                        {
                            db.LoanCollaterals.Add(model.LoanCollateral);
                            db.SaveChanges();
                        }
                        else if (command == AdministrationHelper.DBCommand.Edit.ToString())
                        {
                            db.Entry(model.LoanCollateral).State = System.Data.EntityState.Modified;
                            db.SaveChanges();
                        }
                        else
                        {
                            // cancelled
                        }
                    }
                }

                return RedirectToAction("LoanCoMakersAndCollaterals", "Loan", new { documentNo = model.DocumentNo, borrowerID = model.BorrowerID, branchCode = model.BranchCode });
            }
            catch (Exception ex)
            {
                return View("Error", ex);
            }
        }
    }
}
